package com.yy.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import com.alibaba.fastjson.JSONObject;
/**
 * @Description:
 * @ClassName: OrderProcessor
 * @author: 姚欣宏
 * @date: 2023.11.01
 */
public class OrderProcessor {
    private static final String KEY = "hxgy_jkmh_abcdef";  // AES key & IV
    private static final String CHANNEL_CODE = "PATIENT_WECHAT_APPLET";

    public static void main(String[] args) {
        JSONObject e = new JSONObject();
        e.put("sureOrderVerify", "identifyingCode");
        e.put("sureOrderVerifyInfo", "YOUR_VALUE_HERE");  // replace with actual value
        e.put("sureOrderVerifyIndex", "YOUR_BIZSEQ_HERE"); // replace with actual bizSeq
        e.put("verifyImageCodeType", "WECHAT");
        // You can add more attributes to the object as per your requirements
        JSONObject orderDetails = new JSONObject();
        orderDetails.put("cardId", "YOUR_CARD_ID");
        orderDetails.put("openId", "");
        orderDetails.put("scheduleId", "your_schedule_id_here");
        orderDetails.put("organCode", "your_organ_code_here");
        orderDetails.put("sysTimeArrangeId", "your_sys_time_arrange_id_here");
        orderDetails.put("channelCode", "PATIENT_WECHAT_APPLET");
        // Merge objects
        JSONObject mergedObject = new JSONObject();
        mergedObject.putAll(orderDetails);
        //mergedObject.putAll(e);
        mergedObject.put("channelCode", CHANNEL_CODE);
        String encryptedParameter = encrypt(mergedObject.toString(), KEY, KEY);
        System.out.println(encryptedParameter);
    }
    public static String encrypt(String data) {
        return encrypt(data, KEY, KEY);
    }

    public static String encrypt(String value, String key, String initVector) {
        try {
            IvParameterSpec iv = new IvParameterSpec(initVector.getBytes(StandardCharsets.UTF_8));
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            byte[] encrypted = cipher.doFinal(value.getBytes());
            return Base64.getEncoder().encodeToString(encrypted);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
